
#include <iostream>
#include "bzfsAPI.h"
#include <ruby.h>
#include "bzRuby.h"
#include "bzEvents.h"

BZ_GET_PLUGIN_VERSION


VALUE plugin_list;

//
// Classes
// 

class RubyPluginHandler : public bz_APIPluginHandler
{
	public:

		virtual bool handle (bzApiString plugin, bzApiString param);

};


RubyPluginHandler rb_handler;

CTFCapture capture;
PlayerDie playerdie;
PlayerSpawn playerspawn;
Chat chatmessage;
PlayerJoinPart playerjoinpart;
UnknownSlashCommand unknownslashcommand;
GetPlayerSpawnPos getplayerspawnpos;
AllowPlayer allowplayer;
Tick tick;
GenerateWorld generateworld;
GetPlayerInfo getplayerinfo;
GetAutoTeam getautoteam;
AllowSpawnData allowspawndata;
ListServerUpdateEvent listserverupdateevent;
Ban ban;
HostBan hostban;
Kick kick;

// don't use 'Kill kill' here because 'kill' kills the server for some
// reason (probably a var with the same name) ;)

Kill killa;

PlayerPaused playerpaused;
//MessageFiltered messagefiltered;
GameStartEnd gamestartend;
SlashCommand slashcommand;
//PlayerAuth playerauth;
ServerMsg servermsg;
ShotFired shotfired;
//PlayerUpdate playerupdate;
//NetTransfer nettransfer;
//Loging loging;
ShotEnded shotended;
//FlagTransferred flagtransferred;
FlagGrabbed flaggrabbed;
FlagDropped flagdropped;


bool RubyPluginHandler::handle (bzApiString plugin, bzApiString param)
{
	return load_ruby_plugin(plugin.c_str(), param.c_str());
}


//
// Load/Unload methods
//

BZF_PLUGIN_CALL int bz_Load ( const char* /*commandLine*/ )
{

	// initialize the ruby interpreter
	ruby_init();

	// set the necessary ruby paths
	ruby_init_loadpath();

	// create a array that will contain the plugin references
	plugin_list = rb_ary_new();

	// make sure this object gets registered with the ruby garbage
	// collector
	rb_gc_register_address(&plugin_list);

	// initialize the Plugin superclass
	InitPlugin();

	// register a custom plugin handler for the ruby file extension
	bz_registerCustomPluginHandler ("rb", &rb_handler);

	// register bzflag events
	bz_registerEvent(bz_eCaptureEvent, &capture);
	bz_registerEvent(bz_ePlayerDieEvent, &playerdie);
	bz_registerEvent(bz_ePlayerSpawnEvent, &playerspawn);
	bz_registerEvent(bz_eChatMessageEvent, &chatmessage);
	bz_registerEvent(bz_ePlayerJoinEvent, &playerjoinpart);
	bz_registerEvent(bz_ePlayerPartEvent, &playerjoinpart);
	bz_registerEvent(bz_eUnknownSlashCommand, &unknownslashcommand);
	bz_registerEvent(bz_eGetPlayerSpawnPosEvent, &getplayerspawnpos);
	bz_registerEvent(bz_eAllowPlayer, &allowplayer);
	bz_registerEvent(bz_eTickEvent, &tick);
	bz_registerEvent(bz_eGetWorldEvent, &generateworld);
	bz_registerEvent(bz_eGetPlayerInfoEvent, &getplayerinfo);
	bz_registerEvent(bz_eGetAutoTeamEvent, &getautoteam);
	bz_registerEvent(bz_eAllowSpawn, &allowspawndata);
	bz_registerEvent(bz_eListServerUpdateEvent, &listserverupdateevent);
	bz_registerEvent(bz_eBanEvent, &ban);
	bz_registerEvent(bz_eHostBanEvent, &hostban);
	bz_registerEvent(bz_eKickEvent, &kick);
	bz_registerEvent(bz_eKillEvent, &killa);

	bz_registerEvent(bz_ePlayerPausedEvent, &playerpaused);

	bz_registerEvent(bz_eGameStartEvent, &gamestartend);
	bz_registerEvent(bz_eGameEndEvent, &gamestartend);

	bz_registerEvent(bz_eSlashCommandEvent, &slashcommand);

	bz_registerEvent(bz_eServerMsgEvent, &servermsg);
	bz_registerEvent(bz_eShotFiredEvent, &shotfired);

	bz_registerEvent(bz_eShotEndedEvent, &shotended);

	bz_registerEvent(bz_eFlagGrabbedEvent, &flaggrabbed);
	bz_registerEvent(bz_eFlagDroppedEvent, &flagdropped);

	bz_debugMessage (2,"Ruby plugin loaded");
	return 0;

}

BZF_PLUGIN_CALL int bz_Unload ( void )
{
	emit_ruby_event("destructor");

	bz_removeEvent(bz_eFlagDroppedEvent, &flagdropped);
	bz_removeEvent(bz_eFlagGrabbedEvent, &flaggrabbed);

	bz_removeEvent(bz_eShotEndedEvent, &shotended);

	bz_removeEvent(bz_eShotFiredEvent, &shotfired);
	bz_removeEvent(bz_eServerMsgEvent, &servermsg);

	bz_removeEvent(bz_eSlashCommandEvent, &slashcommand);
	bz_removeEvent(bz_eGameEndEvent, &gamestartend);
	bz_removeEvent(bz_eGameStartEvent, &gamestartend);

	bz_removeEvent(bz_ePlayerPausedEvent, &playerpaused);

	bz_removeEvent(bz_eKillEvent, &killa);
	bz_removeEvent(bz_eKickEvent, &kick);
	bz_removeEvent(bz_eHostBanEvent, &hostban);
	bz_removeEvent(bz_eBanEvent, &ban);
	bz_removeEvent(bz_eListServerUpdateEvent, &listserverupdateevent);
	bz_removeEvent(bz_eAllowSpawn, &allowspawndata);
	bz_removeEvent(bz_eGetAutoTeamEvent, &getautoteam);
	bz_removeEvent(bz_eGetPlayerInfoEvent, &getplayerinfo);
	bz_removeEvent(bz_eGetWorldEvent, &generateworld);
	bz_removeEvent(bz_eTickEvent, &tick);
	bz_removeEvent(bz_eAllowPlayer, &allowplayer);
	bz_removeEvent(bz_eGetPlayerSpawnPosEvent, &getplayerspawnpos);
	bz_removeEvent(bz_eUnknownSlashCommand, &unknownslashcommand);
	bz_removeEvent(bz_ePlayerPartEvent, &playerjoinpart);
	bz_removeEvent(bz_ePlayerJoinEvent, &playerjoinpart);
	bz_removeEvent(bz_eChatMessageEvent, &chatmessage);
	bz_removeEvent(bz_ePlayerSpawnEvent, &playerspawn);
	bz_removeEvent(bz_ePlayerDieEvent, &playerdie);
	bz_removeEvent(bz_eCaptureEvent, &capture);

	rb_gc_unregister_address(&plugin_list);

	ruby_finalize();

	bz_removeCustomPluginHandler ("rb", &rb_handler);

	bz_debugMessage (2,"Ruby plugin unloaded");
	return 0;

}

